﻿using System;
using System.Threading;
using Sportsbook.Domain.Models.Lines;
using Sportsbook.Domain.Repositories;

namespace Sportsbook.Updater.Updaters
{
    public class LinesUpdater
    {
        private readonly ILinesRepository repository;
        private Timer timer;
        private int i = 0;

        public LinesUpdater(ILinesRepository repository)
        {
            this.repository = repository;
        }

        public void Start()
        {
            timer = new Timer(Update, null, 1000, 1000);
        }

        private void Update(object state)
        {
            var random = new Random();
			var trends = Enum.GetValues(typeof(Trend));
            var lines = repository.Get();
            foreach (var line in lines)
            {
                line.Moneyline.HomePrice = random.Next(100, 199);
                line.Moneyline.AwayPrice = random.Next(100, 199);
				line.Moneyline.HomePriceTrend = (Trend)trends.GetValue(random.Next(trends.Length * random.Next(0, 2)));
				line.Moneyline.AwayPriceTrend = (Trend)trends.GetValue(random.Next(trends.Length * random.Next(0, 2)));

                line.Spread.HomePrice = random.Next(100, 199);
                line.Spread.AwayPrice = random.Next(100, 199);
				line.Spread.HomePriceTrend = (Trend)trends.GetValue(random.Next(trends.Length * random.Next(0, 2)));
				line.Spread.AwayPriceTrend = (Trend)trends.GetValue(random.Next(trends.Length * random.Next(0, 2)));

                line.Total.HomePrice = random.Next(100, 199);
                line.Total.AwayPrice = random.Next(100, 199);
				line.Total.HomePriceTrend = (Trend)trends.GetValue(random.Next(trends.Length * random.Next(0, 2)));
				line.Total.AwayPriceTrend = (Trend)trends.GetValue(random.Next(trends.Length * random.Next(0, 2)));

                repository.Save(line);
            }
            i++;
            Console.WriteLine("Lines updated! (" + i +")" ); 
        }
    }
}